﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace WinFormsApp1
{
    public partial class LoginIN : UILoginForm
    {

        public LoginIN()
        {
            InitializeComponent();

        }
        private SqlConnection Connstart()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("XMLFile1.xml");
            string sqlServer = xmlDoc.SelectSingleNode("/DatabaseConfig/SQLServer").InnerText;
            string sqlCatalog = xmlDoc.SelectSingleNode("/DatabaseConfig/SQLCatalog").InnerText;
            string sdeUser = xmlDoc.SelectSingleNode("/DatabaseConfig/SDEUser").InnerText;
            string sdePwd = xmlDoc.SelectSingleNode("/DatabaseConfig/SDEPassword").InnerText;
            string str = "Data Source=" + sqlServer + "; Initial Catalog=sde;User ID=" + sdeUser + ";Password=" + sdePwd;//用户密码登录
            SqlConnection conn = new SqlConnection(str);
            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();
            return conn;
        }
        private void LoginIN_ButtonLoginClick(object sender, EventArgs e)
        {
            string TruePwd = null;
            SqlConnection conn = Connstart();
            String Selectsql = "Select * From orduser where userID = '" + UserName + "'";
            if (UserName == "" || Password == "")
            {
                ShowErrorTip("用户名或密码错误");
            }
            else
            {
                SqlCommand cmd = new SqlCommand(Selectsql, conn);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    TruePwd = reader["Password"].ToString();
                }
                if (Password == TruePwd)
                {
                    ShowSuccessTip("登陆成功");
                    conn.Close();
                    Form1 frm = new Form1();
                    this.Hide();
                    frm.ShowDialog();

                }
                else
                {
                    ShowErrorTip("用户名或密码错误");
                }
            }

        }

        private void LoginIN_ButtonCancelClick(object sender, EventArgs e)
        {
            Registration res = new Registration();
            /*res.Render();*/
            res.ShowDialog();
            if (res.IsOK)
            {
            }
            res.Dispose();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Registration res = new Registration();
            /*res.Render();*/
            res.ShowDialog();
            if (res.IsOK)
            {
            }
            res.Dispose();
        }
    }
}
